$(function () {
    //领料公司加载
    $("[name='lld.llgs.id']").jgbmtreewithnotqx($("[name='lld.llbm.id']"), $("[name='lld.llr.id']"),
        $("#lld_llbm").val(), $("#lld_llr").val())
    //发料公司加载
    $("[name='lld.flgs.id']").jgbmtreewithnotqx($("[name='lld.flbm.id']"), undefined,
        $("#lld_flbm").val(), undefined)
    //加载到货仓库
    $("[name='lld.thck.id']").ckcombobox();

    //新增领料单明细
    $("#lld_lldmx_toolbar [name='xzlldmx']").click(function () {
        var thck_id = $("#lld_thck_id").combobox('getValue');
        var sbgs_id = $("#lld_flgs_id").combobox('getValue');
        if (thck_id)
            searchkhwindow(lldmx_searchWindowOKbtnHandler, thck_id, sbgs_id, sbgs_id, null, null);
        else {
            $('#lldForm').tabs('select', "领料信息");
            $.messager.alert("提示", "请选择提货仓库!", "info");
        }
    });
    //删除领料单明细
    $("#lld_lldmx_toolbar [name='sclldmx']").click(function () {
        var rows = $("#lld_mx").edatagrid('getChecked');
        if (rows) {
            $.messager.confirm('提示', '是否确定删除领料单明细?', function (r) {
                if (r) {
                    $.each(rows, function (i, val) {
                        var index = $("#lld_mx").edatagrid('getRowIndex', val);
                        $("#lld_mx").edatagrid('deleteRow', index);
                    });
                }
            });
        }
        $("#lld_mx").edatagrid('resize');
    });


    $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm':'cd'}, function (cddata) {
        $.post($.webPath() + '/base/sjzdsy/get_sjzdz', {'bm':'jzfs'}, function (data) {
            lldmxGrid($.webPath() + '/kcgl/nbll/nbll_lldmx?lldId=' + $("#lld_content_id").val() || '', cddata, data);
        })
    });

})
function lldmxGrid(href, cdOptions, fsOptions) {
    $("#lld_mx").edatagrid({
        url:href,
        fitColumns:false,
        singleSelect:true,
        fit:false,
        checkbox:true,
        width:1000,
        height:390,
        checkOnSelect:false,
        selectOnCheck:false,
        showFooter:true,
        rownumbers:true,
        toolbar:"#lld_lldmx_toolbar",
        frozenColumns:[
            [
                {field:'checked', checkbox:true},
                {field:'wzgg.code', title:'物资编码', width:100},
                {field:'wzgg.name', title:'物资名称', width:100},
                {field:'wzgg.ggxh', title:'规格型号', width:100},
                {field:'wzgg.dw', title:'单位', width:60}
            ]
        ],
        columns:[
            [
                {field:'wsdj', title:'成本价(不含税)', formatter:function (value) {
                    var sl = formatJe(value, ",");
                    return sl;
                }, width:100},
                {field:'cd.z', title:'产地', width:50},
                {field:'chd', title:'长度', width:50},
                {field:'jz', title:'件重', width:50},
                {field:'kkdsl', title:'可供数量', formatter:function (value) {
                    var sl = formatSl(value, ",");
                    return sl;
                }, width:100},
                {field:'jzfs.id', editor:{type:'combobox', options:{
                    data:fsOptions,
                    valueField:'id',
                    textField:'z',
                    multiple:false,
                    panelHeight:'55'
                }
                }, formatter:function (value) {
                    for (var i = 0; i < fsOptions.length; i++) {
                        if (fsOptions[i].id == value) return fsOptions[i].z;
                    }
                    return value;
                }, title:'计重方式', width:60},
                {field:'kdjs', editor:{type:'numberbox',
                    formatter:function (value) {
                        var sl = formatSlForHtml(value);
                        return sl;
                    }}, title:'件数', width:50},
                {field:'kdsl', editor:{type:'numberbox', options:{min:0, precision:4}}, formatter:function (value) {
                    var sl = formatSl(value, ",");
                    return sl;
                }, title:'数量', width:100},
                {field:'kddj', editor:{type:'numberbox', options:{min:0, precision:4}}, formatter:function (value) {
                    var sl = formatJe(value, ",");
                    return sl;
                }, title:'不含税价(元)', width:100},
                {field:'bz', editor:{type:'text'}, title:'备注', width:100}
            ]
        ],
        onSelect:function (rowIndex) {
            //获取每个单元格
            $("#lld_mx").edatagrid('editRow', rowIndex);
            var editors = $("#lld_mx").datagrid('getEditors', rowIndex);
            if (editors.length == 0)
                return;
            var kdslEditor = editors[2];
            var kddjEditor = editors[3];
            kdslEditor.target.unbind('blur').bind('blur', function () {
                $(kdslEditor.target).numberbox('setValue', $(kdslEditor.target).val());
                $(kddjEditor.target).numberbox('setValue', $(kddjEditor.target).val());
                countlldSum(rowIndex, $(kdslEditor.target).numberbox('getValue'), $(kddjEditor.target).numberbox('getValue'));
            });
            kddjEditor.target.unbind('blur').bind('blur', function () {
                $(kdslEditor.target).numberbox('setValue', $(kdslEditor.target).val());
                $(kddjEditor.target).numberbox('setValue', $(kddjEditor.target).val());
                countlldSum(rowIndex, $(kdslEditor.target).numberbox('getValue'), $(kddjEditor.target).numberbox('getValue'));
            });
        }
    })
}

//查询卡号物资确定按钮回调函数
function lldmx_searchWindowOKbtnHandler(data) {
    var rowdatas = $.map(data, function (n, i) {
        if (n.kkdsl && n.kkdsl != 0) {
            return {
                'kh.id':n.id,
                'kh.khbm':n.khbm,
                'wzgg.id':n.jtwz_id,
                'wzgg.code':n.jtwz_code,
                'wzgg.name':n.jtwz_name,
                'wzgg.ggxh':n.jtwz_ggxh,
                'wzgg.dw':n.jtwz_dw,
                'chd':n.chd,
                'jz':n.jz,
                'wsdj':n.wsdj,
                'cd.id':n.cd_id,
                'cd.z':n.cd_z,
                'kkdsl':n.kkdsl,
                'jzfs.id':null,
                'kdjs':1,
                'kdsl':n.sl,
                'kddj':n.wsdj,
                'bz':''
            };
        }
    });
    $.each(rowdatas, function () {
        $("#lld_mx").edatagrid('appendRow', this);
    });
}

//计算开单数量和开单金额
function countlldSum(index, count, je) {
    var sumkdsl = 0;
    var sumkddj = 0;
    var data = $("#lld_mx").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var kdsl = parseFloat(data.rows[i]['kdsl']);
        var kddj = parseFloat(data.rows[i]['kddj']);
        if (i == index) {
            kdsl = parseFloat(count);
            kddj = parseFloat(je);
        } else {
            kdsl = isNaN(kdsl) ? 0 : kdsl;
            kddj = isNaN(kddj) ? 0 : kddj;
        }
        sumkdsl += kdsl;
        sumkddj += kddj;
    }
    addFooter(sumkdsl, sumkddj);
}

//在表底加载开单数量和开单金额的合计
function addFooter(sumkdsl, sumkddj) {
    var footer = [
        {"wzgg.code":"合计", "kdsl":sumkdsl, "kddj":sumkddj}
    ];
    if (footer != null)
        $("#lld_mx").edatagrid('reloadFooter', footer);
}

